<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/detail.css">

</head>

<body>
    <div class="nav">
        <img src="pic/logo.jpg" alt="">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
        <a class="nav-span" href="#" onclick="logout()">注销</a>
    </div>

    <div class="container">
        <div class="left">
            <div class="card">
                <img src="pic/doge.jpg" alt="">
                <h3>比特汤老师</h3>
                <a href="#">GitHub 地址</a>
                <div class="row">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="row">
                    <span id="span-article">2</span>
                    <span id="span-sort">1</span>
                </div>
            </div>
        </div>
        <div class="right">
            <div class="content">
                <div class="title">我的第一篇博客</div>
                <div class="date">2021-06-02</div>
                <div class="detail">
                    <p>从今天开始, 好好学习~Lorem, ipsum dolor sit amet consectetur adipisicing elit. Obcaecati cumque alias
                        laborum numquam aliquam? Ipsam aliquam dolorem officiis! Magni pariatur officiis iusto? Et unde
                        quo fuga, minus deserunt architecto eligendi.</p>
                    <p>从今天开始, 好好学习~Lorem, ipsum dolor sit amet consectetur adipisicing elit. Obcaecati cumque alias
                        laborum numquam aliquam? Ipsam aliquam dolorem officiis! Magni pariatur officiis iusto? Et unde
                        quo fuga, minus deserunt architecto eligendi.</p>
                    <p>从今天开始, 好好学习~Lorem, ipsum dolor sit amet consectetur adipisicing elit. Obcaecati cumque alias
                        laborum numquam aliquam? Ipsam aliquam dolorem officiis! Magni pariatur officiis iusto? Et unde
                        quo fuga, minus deserunt architecto eligendi.</p>
                </div>
                <div class="operating">
                    <button onclick="window.location.href='blog_update.html?id=${blog.id}'">编辑</button>
                    <button onclick="deleteBlog()">删除</button>
                </div>
            </div>

        </div>
    </div>

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="blog-editormd/css/editormd.css" />
    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/lib/marked.min.js"></script>
    <script src="blog-editormd/lib/prettify.min.js"></script>
    <script src="blog-editormd/editormd.js"></script>
    <script src="js/common.js"></script>
    <script>
        // 填充个人信息
        getUserInfo();
        // 填充博客数量和分类
        getBlogInfo();
        // 填充右侧完整的博客信息
        getBlogDetail();
        function getBlogDetail() {
            // 获取URL中的id信息，继而通过Ajax请求来获取对应的文章信息
            let id = new URLSearchParams(window.location.search).get('id');
            $.ajax({
                type: "get",
                url: "/blog/getBlogDetail",
                data: {
                    articleId: id
                },
                success(result) {
                    if (result.data != null) {
                        // 先清空右侧内容
                        $('.container .right .content').empty();
                        // 动态生成HTML
                        let blog = result.data;
                        let blogHtml = `
                            <div class="blog">
                                <div class="title">${blog.articleName}</div>
                                <div class="date">${blog.createTimeFormat}</div>
                                <div id="md-content"></div>
                            </div>
                        `;
                        $('.container .right .content').append(blogHtml);
                        let button = `
                            <div class="operating">
                                <button onclick="window.location.href='blog_update.html?id=${blog.id}'">编辑</button>
                                <button onclick="deleteBlog()">删除</button>
                            </div>
                        `;
                        $('.container .right .content').append(button);
                        // 渲染Markdown为HTML
                        editormd.markdownToHTML("md-content", {
                            markdown: blog.content,
                            htmlDecode: "style,script,iframe",
                            emoji: true,
                            taskList: true,
                            tex: true,
                            flowChart: true,
                            sequenceDiagram: true,
                        });                        
                    }
                },
                error(xhr) {
                    if (xhr.status == 401) {
                        alert('用户未登录');
                        location.assign('./blog_login.html');
                    } else {
                        alert('HTTP请求失败');
                    }
                }
            });

        }

        function deleteBlog() {
            let id = new URLSearchParams(window.location.search).get('id');
            // 这里可以用 id 发送 Ajax 请求删除
            if (confirm("确定要删除这篇博客吗？")) {
                $.ajax({
                    type: "delete",
                    url: "/blog/deleteBlog",
                    data: { 
                        articleId: id 
                    },
                    success(result) {
                        if (result.data) {
                            alert("删除成功");
                            location.assign('./blog_list.html');
                        } else {
                            alert("删除失败");
                        }
                    },
                    error() {
                        alert("HTTP请求失败");
                    }
                });
            }
        }

    </script>
</body>

</html>